package com.xinnet.mms.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sun.util.logging.resources.logging;

import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.allot.service.CustomAllotService;
import com.xinnet.mms.allot.service.CustomMixService;
import com.xinnet.mms.customerManage.service.CustomerManageService;
import com.xinnet.mms.deployapproval.service.CustomDeployApprovalService;
import com.xinnet.mms.dept.service.DeptService;
import com.xinnet.mms.entity.CustomMixAudit;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.Role;
import com.xinnet.mms.login.service.EmpService;
import com.xinnet.mms.utils.CustomerManage;





@Controller
@RequestMapping(value = "/ruleSet/approval/customDeployApproval")
public class CustomDeployApprovalController{
	@Autowired
	private EmpService empService;
	@Autowired
	private CustomerManageService customerManageService;
	@Autowired
	private CustomAllotService customAllotService;
	@Autowired
	private CustomDeployApprovalService customDeployApprovalService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private CustomMixService customMixService;
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/customDeployApprovalPage")
	public String customDeployApprovalPage(HttpServletRequest request,HttpServletResponse response){
		Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
		String approvalerId = currentUser.getEmpno();
		Role role = (Role) request.getSession().getAttribute("role");
		String deptId = currentUser.getId();
		if (role.getName().equals("总经理") || role.getName().equals("副总经理")) {
			deptId=null;
		}
		//String deptIdStr = deptService.getChildNodeId(deptId);
		try {
			customMixService.updateCustomflagByEmpno(currentUser.getEmpno());
		} catch (SQLException e) {
			e.printStackTrace();
			return "/ruleSet/approval/customDeployApproval";
		}
		Map<String,Object> paramMap = new HashMap<String, Object>();
		Map<String,Object> paramToBath = new HashMap<String, Object>();
		PageView pageView = new PageView();
		int pageSize = pageView.getPageSize();
		String pageNow = request.getParameter("pageNow");
		paramMap.put("deptno", deptId);
		paramToBath.put("deptno", deptId);
		Integer limitTop=0;
		Integer limitDown=pageSize;
		if(pageNow==null || "".equals(pageNow)){
			limitTop=0;
		}else{
			limitTop=Integer.parseInt(pageNow)*pageSize-pageSize;
		}
		paramMap.put("limitTop", limitTop);
		paramMap.put("limitDown", limitDown);
		paramMap.put("deptId", deptId);
		paramMap.put("approvalerId", approvalerId);
		paramToBath.put("deptId", deptId);
		paramToBath.put("approvalerId", approvalerId);
		List<CustomMixAudit> customMixAuditListTemp = customerManageService.getCustomDeployApprovalItemsByMap(paramMap);
		List<CustomMixAudit> customMixAuditList2Temp = customerManageService.getCustomDeployApprovalItemsByMap(paramToBath);
		int count = customerManageService.getCustomDeployApprovalItemsCountByMap(paramMap);
		//去除自己申请的结果集
		List<CustomMixAudit> customMixAuditList = new ArrayList<CustomMixAudit>();
		if (role.getName().equals("区域总监")) {
			
			for (CustomMixAudit customMixAudit : customMixAuditListTemp) {
				
				String empNo = currentUser.getEmpno();
				
				if(!empNo.equals(customMixAudit.getApplyerId())){
					customMixAuditList.add(customMixAudit);
				}
			}
			count=count-(customMixAuditListTemp.size()-customMixAuditList.size());
		} else {
			customMixAuditList.addAll(customMixAuditListTemp);
		}
		
		
		
		List<CustomMixAudit> customMixAuditList2 = new ArrayList<CustomMixAudit>();
		if (role.getName().equals("区域总监")) {
			
			for (CustomMixAudit customMixAudit : customMixAuditList2Temp) {
				
				String empNo = currentUser.getEmpno();
				
				if(!empNo.equals(customMixAudit.getApplyerId())){
					customMixAuditList2.add(customMixAudit);
				}
			}
			count=count-(customMixAuditList2Temp.size()-customMixAuditList2.size());
		} else {
			customMixAuditList2.addAll(customMixAuditList2Temp);
		}
		
		StringBuffer sb = new StringBuffer();
		for (CustomMixAudit customMixAudit : customMixAuditList2) {
			sb.append(customMixAudit.getId());
			sb.append(",");
		}
		if(!"".equals(sb.toString())){
			sb.deleteCharAt(sb.length()-1);
		}
				
		pageView.setPageSize(pageSize);
		if(pageNow==null || "".equals(pageNow)){
			pageView.setPageNow(1);
		}else{
			pageView.setPageNow(Integer.parseInt(pageNow));
		}
		Integer pageCount=count%pageSize;
		pageView.setPageCount(pageCount);
		pageView.setRowCount(count);
		pageView.setQueryResult(count, customMixAuditList);
		request.setAttribute("isQuery", false);
		request.setAttribute("pageView", pageView);
		request.setAttribute("bathAllCusId", sb.toString());
		return "/ruleSet/approval/customDeployApproval";
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/getCustomMixAudit")
	public String getCustomMixAudit(HttpServletRequest request,HttpServletResponse response){
		Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
		String approvalerId = currentUser.getEmpno();
		Role role = (Role) request.getSession().getAttribute("role");
		String deptId = currentUser.getId();
		if (role.getName().equals("总经理") || role.getName().equals("副总经理")) {
			deptId=null;
		}
		//String deptIdStr = deptService.getChildNodeId(deptId);
		
		String customerId = request.getParameter("customerId");
		if(customerId!=null){
			customerId=customerId.trim();
		}
		String contact = request.getParameter("contact");
		if(contact!=null){
			contact=contact.trim();
		}
		String phone = request.getParameter("phone");
		if(phone!=null){
			phone=phone.trim();
		}
		String email = request.getParameter("email");
		if(email!=null){
			email=email.trim();
		}
		Map<String,Object> param = new HashMap<String,Object>();
		Map<String,Object> paramToBath = new HashMap<String,Object>();
		PageView queryPageView = new PageView();
		param.put("pageView", queryPageView);
		param.put("customerId", customerId);
		param.put("contact", contact);
		param.put("phone", phone);
		param.put("email", email);
		param.put("approvalerId", approvalerId);
		//param.put("deptIdStr", deptIdStr);
		param.put("deptno", deptId);
		
		
		paramToBath.put("pageView", queryPageView);
		paramToBath.put("customerId", customerId);
		paramToBath.put("contact", contact);
		paramToBath.put("phone", phone);
		paramToBath.put("email", email);
		paramToBath.put("approvalerId", approvalerId);
		//param.put("deptIdStr", deptIdStr);
		paramToBath.put("deptno", deptId);
		
		paramToBath.put("limitTop", 0);
		paramToBath.put("limitDown", 1000000);
		paramToBath.put("deptId", deptId);
		int pageSize = queryPageView.getPageSize();
		String pageNow = request.getParameter("pageNow");
		param.put("deptno", deptId);
		Integer limitTop=0;
		Integer limitDown=pageSize;
		if(pageNow==null || "".equals(pageNow)){
			limitTop=0;
		}else{
			limitTop=Integer.parseInt(pageNow)*pageSize-pageSize;
		}
		param.put("limitTop", limitTop);
		param.put("limitDown", limitDown);
		param.put("deptId", deptId);
		List<CustomMixAudit> customMixAuditListTemp = customerManageService.getCustomDeployApprovalItemsByMap(param);
		List<CustomMixAudit> customMixAuditList2Temp = customerManageService.getCustomDeployApprovalItemsByMap(paramToBath);
		int count = customerManageService.getCustomDeployApprovalItemsCountByMap(param);
		//去除自己申请的结果集
		List<CustomMixAudit> customMixAuditList = new ArrayList<CustomMixAudit>();
		if (role.getName().equals("区域总监")) {
			
			for (CustomMixAudit customMixAudit : customMixAuditListTemp) {
				
				String empNo = currentUser.getEmpno();
				
				if(!empNo.equals(customMixAudit.getApplyerId())){
					customMixAuditList.add(customMixAudit);
				}
			}
			count=count-(customMixAuditListTemp.size()-customMixAuditList.size());
		} else {
			customMixAuditList.addAll(customMixAuditListTemp);
		}
		
		List<CustomMixAudit> customMixAuditList2 = new ArrayList<CustomMixAudit>();
		if (role.getName().equals("区域总监")) {
			
			for (CustomMixAudit customMixAudit : customMixAuditList2Temp) {
				
				String empNo = currentUser.getEmpno();
				
				if(!empNo.equals(customMixAudit.getApplyerId())){
					customMixAuditList2.add(customMixAudit);
				}
			}
			
		} else {
			customMixAuditList2.addAll(customMixAuditList2Temp);
		}
		StringBuffer sb = new StringBuffer();
		for (CustomMixAudit customMixAudit : customMixAuditList2) {
			sb.append(customMixAudit.getId());
			sb.append(",");
		}
		if(!"".equals(sb.toString())){
			sb.deleteCharAt(sb.length()-1);
		}
		
		queryPageView.setPageSize(pageSize);
		if(pageNow==null || "".equals(pageNow)){
			queryPageView.setPageNow(1);
		}else{
			queryPageView.setPageNow(Integer.parseInt(pageNow));
		} 
		Integer pageCount=count%pageSize;
		queryPageView.setPageCount(pageCount);
		queryPageView.setRowCount(count);
		queryPageView.setQueryResult(count, customMixAuditList);
		
		request.setAttribute("isQuery", true);
		request.setAttribute("pageView", queryPageView);
		request.setAttribute("customerId",customerId);
		request.setAttribute("contact",contact);
		request.setAttribute("phone",phone);
		request.setAttribute("email",email);
		
		request.setAttribute("bathAllCusId", sb.toString());
		return "/ruleSet/approval/customDeployApproval";
	}
	/**
	 * 客户调配审批
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	@ResponseBody
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/approval")
	public String approval(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
		String customMixId = request.getParameter("id");
		String deptId = currentUser.getId();
		//String deptIdStr = deptService.getChildNodeId(deptId);
		String isEffect = request.getParameter("isEffect");
		String approvalerId = currentUser.getEmpno();
		String productId = request.getParameter("productId");
		String productCode = request.getParameter("productCode");
		int result = customDeployApprovalService.approval(customMixId,isEffect,approvalerId,deptId,productId, productCode);
		return result+"";  //"redirect:/ruleSet/approval/customDeployApproval/customDeployApprovalPage";
	}
	/**
	 * 客户调配批量审批
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	@ResponseBody
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/bathApproval")
	public String bathApproval(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
		String approvalerId = currentUser.getEmpno();
		String deptId = currentUser.getId();
		//String deptIdStr = deptService.getChildNodeId(deptId);
		String customMixIdStr = request.getParameter("idStr");
		String isEffect = request.getParameter("isEffect");
		String[]  customMixIdArray = customMixIdStr.split(",");
		int result = 0;
		for(int i=0;i<customMixIdArray.length;i++) {
			CustomMixAudit cma = customMixService.getCustomMixAuditById(customMixIdArray[i]);
			result = customDeployApprovalService.approval(customMixIdArray[i],isEffect,approvalerId,deptId,cma.getProductId(), cma.getProductCode());
		}
//		int result = customDeployApprovalService.bathApproval(customMixIdStr,isEffect,approvalerId,deptId);
		return result+""; 
		//return "redirect:/ruleSet/approval/customDeployApproval/customDeployApprovalPage";
	}
}
